package com.wx.wxpush.service;

import com.wx.wxpush.entiy.Cron;
import com.wx.wxpush.entiy.TempData;
import com.wx.wxpush.entiy.TemplateData;
//import com.wx.wxpush.job.SpringDynamicCornTask;
//import com.wx.wxpush.job.ScheduledConfig;
import com.wx.wxpush.job.SpringDynamicCornTaskNight;
import com.wx.wxpush.mapper.CornTaskMapper;
import com.wx.wxpush.mapper.PushersMapper;
import com.wx.wxpush.util.Pusher;
import com.wx.wxpush.util.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Controller
@RequestMapping("/wxpushs")
@Component
public class PushService {
        @Autowired
        private PushersMapper pushersMapper;
//    @Autowired
//    private SpringDynamicCornTask springDynamicCornTask;
    @Autowired
    private SpringDynamicCornTaskNight springDynamicCornTaskNight;
    @Autowired
    private CornTaskMapper cornTaskMapper;

        String openid="o7Rzm6D3Q8WqEK6jI-wz4jK_ZhuM";
        //要推送的用户openid
        List<String> list=new ArrayList<>();
       List<TemplateData> sys=new ArrayList<>();

       //定时器
//        @Scheduled(cron = "* * * * * ?")
//     public void Scheduled(){
//            System.out.println("进去自动定时阶段");
//            ScheduledConfig scheduledConfig= new ScheduledConfig() ;
//          scheduledConfig.selectList();
//
//        }
    public ResultVO goodMoringOk(String crons){
        List<Cron> list1=new ArrayList<>();

        list1=cornTaskMapper.selectByCrons(crons);
        for (Cron openids:list1){
            System.out.println(openids+"openids");
            if(openids.getMoringStatus().equals(1)||openids.getMoringStatus()==1){
            TempData tt = pushersMapper.selectByOpenids(openids.getOpenid());
            System.out.println(tt + "rrrrrrrrttt");
            //发送 方法
            Pusher.pushMorning(tt);
            }
            else {
                return ResultVO.fail();
            }

        }
        return  ResultVO.ok();
    }

    public ResultVO goodNightOk(String crons){
        List<Cron> list1=new ArrayList<>();

        list1=cornTaskMapper.selectByNightCrons(crons);
        for (Cron openids:list1){
            System.out.println(openids+"openids");
            if(openids.getNightStatus().equals(1)||openids.getNightStatus()==1){
            TempData tt = pushersMapper.selectByOpenids(openids.getOpenid());
            System.out.println(tt + "rrrrrrrrttt");
            //发送 方法
            Pusher.pushMorning(tt);}
            else {
                return ResultVO.fail();
            }
        }
        return ResultVO.ok();
    }


        public void goodMorning(String crons){
            List<String> list1=new ArrayList<>();
//            List<String> list=new ArrayList<>();
            //list = cornTaskMapper.selectList(openid);
//            for (String morcons:list){
//                System.out.println(morcons+"iiiiiiii");
//                springDynamicCornTask.setCron(cronMoring);
//            }
        list=pushersMapper.selectAllOpenid();
            System.out.println(list+"list");
        for(String openid:list){
            System.out.println(openid+"openid");
           //String cron= springDynamicCornTask.getCron();
           List<String> cron = cornTaskMapper.selectList();
           for ( String s :cron) {
               if (s == crons) {
                   TempData tt = pushersMapper.selectByOpenid(openid, s);
                   System.out.println(tt + "rrrrrrrrttt");
                   //发送 方法
                   Pusher.pushMorning(tt);
               }
           }
        }
        }

    public void goodMorningtest(String openid, String cron) {
        System.out.println(openid + "openid");
        TempData tt = pushersMapper.selectByOpenid(openid, cron);
        if (tt==null || tt.getOpenid()==null || tt.getAppid()==null ||
                tt.getEveTemplateId()==null || tt.getSecret()==null) {
            System.out.println("相等字段没有对应，无法推送");
            ResultVO.fail("相等字段没有对应，无法推送");
        } else {
            System.out.println(tt + "rrrrrrrrttt");
            //发送 方法
            int n = Pusher.pushMorning(tt);
        }

        ResultVO.ok("发送成功");
    }


    public void goodNighttest(String openid, String cornnight) {
        System.out.println(openid + "openid");
        TempData tt= pushersMapper.selectByOpenidNight(openid,cornnight);
        if (tt==null || tt.getOpenid()==null || tt.getAppid()==null ||
                tt.getEveTemplateId()==null || tt.getSecret()==null) {
            System.out.println("相等字段没有对应，无法推送");
            ResultVO.fail("相等字段没有对应，无法推送");
        } else {
            System.out.println(tt + "rrrrrrrrttt");
            //发送 方法
            Pusher.pushMorning(tt);
        }

        ResultVO.ok("发送成功");


    }
}



//        @Scheduled(cron = "0 0 19 * * ?")
//        public void goodNight(){
//            Pusher.pushNight(openid);
//        }



